package dao;

import java.sql.Connection;
import java.util.List;

import org.apache.log4j.Logger;

import util.ConnectionPool;
import util.SqlOperation;
import exception.SystemLevelException;

/**
 * 基础Dao，提供一些公用的方法
 * @author Administrator
 *
 */
@SuppressWarnings("rawtypes")
public abstract class DangDao {
	private static final String PREFIX = "dang-";
	private static final String SUFFIX = ".xml";
	private static SqlOperation sqlOperation = new SqlOperation();
	private static Logger log = Logger.getLogger(DangDao.class);
	
	protected abstract String getNameSpace();
	
	private String getStatement(String sqlId){
		return PREFIX + getNameSpace() + SUFFIX + sqlId;
	}
	
	private Connection getConnection(){
		return ConnectionPool.getConnection();
	}
	
	private void closeConnection(Connection conn){
		ConnectionPool.closeConnection(conn);
	}
	
	/**
	 * 根据查询条件查询出单个对象或单行单列的数据
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	protected Object queryForObject(String sqlId, Object obj) throws Exception{
		Connection conn = getConnection();
		Object result = null;
		try {
			result = sqlOperation.queryForObject(conn, getStatement(sqlId), obj);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new SystemLevelException(e.getMessage());
		} finally {
			closeConnection(conn);
		}
		return result;
	}
	
	/**
	 * 根据查询条件查询出列表数据
	 * @param obj
	 * @return
	 */
	protected List<?> queryForList(String sqlId, Object obj) throws Exception{
		Connection conn = getConnection();
		List result = null;
		try {
			result = sqlOperation.queryForList(conn, getStatement(sqlId), obj);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new SystemLevelException(e.getMessage());
		} finally {
			closeConnection(conn);
		}
		return result;
	}
	/**
	 * 根据sql查询数据
	 * @param sql
	 * @param param
	 * @return
	 */
	protected List queryBySql(String sqlId, Object[] params) throws Exception {
		Connection conn = getConnection();
		List result = null;
		try {
			result = sqlOperation.queryForList(conn, getStatement(sqlId), params);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new SystemLevelException(e.getMessage());
		} finally {
			closeConnection(conn);
		}
		return result;
	}
	
	protected Integer insert(String sqlId, Object obj) throws Exception{
		Connection conn = getConnection();
		Integer pk = null;
		try {
			pk = Integer.parseInt(sqlOperation.insert(conn, getStatement(sqlId), obj).toString());
			return pk;
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new SystemLevelException(e.getMessage());
		} finally {
			closeConnection(conn);
		}
	}
	
	protected void update(String sqlId, Object obj) throws Exception {
		Connection conn = getConnection();
		try {
			sqlOperation.update(conn, getStatement(sqlId), obj);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new SystemLevelException(e.getMessage());
		} finally {
			closeConnection(conn);
		}
	}
	
}
